package com.pacy.pacybaseproject.activitys;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.animation.Animation;

import com.gyf.immersionbar.ImmersionBar;
import com.pacy.pacybaseproject.BR;
import com.pacy.pacybaseproject.R;
import com.pacy.pacybaseproject.base.BaseXActivity;
import com.pacy.pacybaseproject.databinding.ActivityExchangeDetailBinding;
import com.pacy.pacybaseproject.viewmodels.ExchangeDetailViewModel;
import com.xuexiang.xui.utils.StatusBarUtils;
import com.xuexiang.xui.utils.ViewUtils;
import com.xuexiang.xui.widget.statelayout.SimpleAnimationListener;

import io.reactivex.annotations.NonNull;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.ScreenUtil;

/**
 * 兑换详情
 */
public class ExchangeDetailActivity extends BaseXActivity<ActivityExchangeDetailBinding, ExchangeDetailViewModel> {

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_exchange_detail;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        ImmersionBar.with(this).statusBarDarkFont(true).init();



        viewModel.title.setValue(getIntent().getExtras().getString("title"));
        viewModel.scroNum.setValue(getIntent().getExtras().getString("scroNum"));
        viewModel.priceNum.setValue(getIntent().getExtras().getString("priceNum"));
        viewModel.slogen.setValue(getIntent().getExtras().getString("slogen"));

        binding.baseLeftIcon.setOnClickListener(v -> onBackPressed());
        ViewUtils.setPaddingTop(binding.toolbarRl, StatusBarUtils.getStatusBarHeight(ExchangeDetailActivity.this));
        initTextView();
        binding.scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                KLog.i("scrollX="+scrollX+"  oldScrollX="+oldScrollX+"  scrollY="+scrollY+"  oldScrollY="+oldScrollY);
                    if(scrollY>200){
                        if(binding.baseTitle.getVisibility()==View.GONE){
                            ViewUtils.fadeIn(binding.baseTitle,100,new SimpleAnimationListener(){
                                @Override
                                public void onAnimationStart(Animation animation) {
                                    super.onAnimationStart(animation);
                                    binding.toolbarRl.setBackgroundColor(Color.WHITE);
                                }
                            });

//                            ViewUtils.playViewBackgroundAnimation(binding.toolbarRl,Color.TRANSPARENT,Color.WHITE,1000);
                        }

                    }else{
                        if(binding.baseTitle.getVisibility()==View.VISIBLE){
                            ViewUtils.fadeOut(binding.baseTitle,100,new SimpleAnimationListener(){
                                @Override
                                public void onAnimationStart(Animation animation) {
                                    super.onAnimationStart(animation);
                                    binding.toolbarRl.setBackgroundColor(Color.TRANSPARENT);
                                }
                            });
//                            ViewUtils.playViewBackgroundAnimation(binding.toolbarRl,Color.WHITE,Color.TRANSPARENT,1000);
                        }

                    }

            }
        });
    }

    /**
     * 协议配置
     */
    private void initTextView() {
        String agreement = "7、红包使用规则，公交活动参与城市及使用范围：点我查看";
        SpannableStringBuilder span = new SpannableStringBuilder(agreement);
        span.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(ContextCompat.getColor(ExchangeDetailActivity.this, R.color.colorPrimary));
            }
        }, agreement.indexOf("：")+1, agreement.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

        binding.dwckTv.setMovementMethod(LinkMovementMethod.getInstance());
        binding.dwckTv.setText(span);
    }

}